home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / MENULIST.CLS < prev    next >
Text File  |  1997-06-14  |  5KB  |  168 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CMenuList"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11.  
  12. Private nItems As New Collection
  13. Private hMenu As Long, idMax As Long
  14. ' Warning! These members should be static--that is, one variable
  15. ' available to all objects of the given type. The entire menu tree
  16. ' has only one window handle. But Basic doesn't have static
  17. ' variables, so you must fake them.
  18. Private hWnd As Long, fSys As Boolean
  19. ' Warning! Violation of encapsulation standards! This property
  20. ' should not be public, but its partner class won't work otherwise.
  21. ' Don't use this property directly.
  22. Public Parent As CMenuList
  23.  
  24. Sub Class_Initialize()
  25.     ' Make sure Parent property is initialized
  26.     Set Parent = Nothing
  27. End Sub
  28.  
  29. Sub Class_Terminate()
  30.     DestroyMenus
  31. End Sub
  32.  
  33. ' Call this by passing window handle (it can take menu handle, but
  34. ' should never get one through caller)
  35. Function Create(hA As Long, Optional fSysA As Boolean = False) As Boolean
  36.     fSys = fSysA
  37.     If IsWindow(hA) Then
  38.         ' Create system or normal menu from hWnd
  39.         If fSys Then
  40.             hMenu = GetSystemMenu(hA, False)
  41.         Else
  42.             hMenu = GetMenu(hA)
  43.         End If
  44.         hWnd = hA
  45.     Else
  46.         ' Don't accept menu handle from top node
  47.         If IsMenu(hA) And Parent Is Nothing Then Exit Function
  48.         hMenu = hA: hWnd = WinHandle
  49.     End If
  50.  
  51.     ' Create each item in list and add to collection
  52.     Dim item As CMenuItem, i As Long, f As Boolean
  53.     DestroyMenus
  54.     For i = 0 To Count - 1
  55.         Set item = New CMenuItem
  56.         ' Create will also create new submenus
  57.         f = item.Create(i, hMenu, Me)
  58.         BugAssert f     ' Should never fail
  59.         nItems.Add item
  60.         ' Needed by InsertNew method
  61.         If item.ID > idMax Then idMax = item.ID
  62.     Next
  63.     Create = True
  64. End Function
  65.  
  66. ' Redraw after each change
  67. Sub ReDraw()
  68.     DrawMenuBar hWnd
  69. End Sub
  70.  
  71. ' Insert new item, moving everything down
  72. Function InsertNew(sItem As String, Optional iPos As Long = 0, _
  73.                    Optional afFlags As Long = MF_STRING Or MF_DISABLED) As Boolean
  74.     If iPos <= 0 Then iPos = Count   ' Append to end
  75.     InsertNew = InsertMenu(hMenu, iPos, MF_BYPOSITION Or afFlags, _
  76.                            idMax + 10, sItem)
  77.     Dim f As Boolean, item As CMenuItem
  78.     f = item.Create(CInt(iPos), hMenu, Me)
  79.     BugAssert f     ' Should never fail
  80.     nItems.Add item
  81.     If item.ID > idMax Then idMax = item.ID
  82. End Function
  83.  
  84. ' Move up through recursive levels and recreate window from top
  85. Function Refresh()
  86.     If Parent Is Nothing Then
  87.         Refresh = Create(hWnd, fSys)
  88.     Else
  89.         Refresh = Parent.Refresh
  90.     End If
  91. End Function
  92.  
  93. ' Number of items in menu
  94. Property Get Count() As Integer
  95.     Count = GetMenuItemCount(hMenu)
  96.     If Count = -1 Then Count = 0
  97. End Property
  98.  
  99. ' Move up through recursive levels to find top level window handle
  100. ' (static member would be much easier)
  101. Property Get WinHandle() As Long
  102.     If Parent Is Nothing Then
  103.         WinHandle = hWnd
  104.     Else
  105.         WinHandle = Parent.WinHandle
  106.     End If
  107. End Property
  108.  
  109. Property Get SysMenu() As Boolean
  110.     If Parent Is Nothing Then
  111.         SysMenu = fSys
  112.     Else
  113.         SysMenu = Parent.SysMenu
  114.     End If
  115. End Property
  116.  
  117. ' Clear everything from here on down
  118. Public Sub DestroyMenus()
  119.     Dim item As CMenuItem
  120.     For Each item In nItems
  121.         If item.Popup Then item.Child.DestroyMenus
  122.         Set item = Nothing
  123.         nItems.Remove 1
  124.     Next
  125. End Sub
  126.  
  127. ' Find menu item by its string name
  128. Function Find(sName As String, item As CMenuItem) As Boolean
  129.     Find = True
  130.     Dim i As Integer
  131.     ' Step through each item, searching for match
  132.     For Each item In nItems
  133.         ' Test against name (stripped version of text)
  134.         If item.Name Like sName Then Exit Function
  135.         If item.Popup Then
  136.             ' Recurse through any submenus
  137.             If item.Child.Find(sName, item) Then Exit Function
  138.         End If
  139.     Next
  140.     ' If we got all way through, it's not there
  141.     Find = False
  142. End Function
  143.  
  144. Sub Walk(Optional iLevel As Integer = 0)
  145.     Dim item As CMenuItem
  146.     For Each item In nItems
  147.         ' Walk through current list until user says stop
  148.         If Not MenuWalker(item, iLevel) Then Exit Sub
  149.         ' Recurse through submenus
  150.         If item.Popup Then item.Child.Walk iLevel + 1
  151.     Next
  152. End Sub
  153.     
  154. #If fMenuWalker = 0 Then
  155. Function MenuWalker(item As CMenuItem, iLevel As Integer) As Boolean
  156.     Dim s As String
  157.     s = "Name: " & item.Name & " ( "
  158.     s = s & IIf(item.Disabled, "Disabled ", "")
  159.     s = s & IIf(item.Checked, "Checked ", "")
  160.     s = s & IIf(item.Grayed, "Grayed ", "")
  161.     s = s & IIf(item.Popup, "Popup ", "") & ")"
  162.     Debug.Print String$(iLevel, sTab) & s
  163.     ' Your MenuWalker can return False to stop walk
  164.     MenuWalker = True
  165. End Function
  166. #End If
  167. '
  168.